
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DTO;
using BUS;
using System.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Drawing.Imaging;
namespace QuanLyPhongMach
{
    public partial class frmThemBenhNhan : DevComponents.DotNetBar.Metro.MetroForm
    {
        public delegate void ThemMoi();
        public event ThemMoi KhiThemMoi = null;
        public frmThemBenhNhan()
        {
            InitializeComponent();
        }

        OpenFileDialog ofd = new OpenFileDialog();
        string fileName = "";
        private void btnChonHinh_Click(object sender, EventArgs e)
        {
            ofd.Title = "Chọn hình";
            ofd.Filter = "Image|*.jpg;*.jpeg;*.gif;*.bmp";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string[] file = ofd.FileName.Split('\\');
                fileName = file.Last();
                string Source = ofd.FileName;
                string Destination = Environment.CurrentDirectory + @"\HinhAnh" + @"\" + fileName;
                if (System.IO.Directory.Exists(Environment.CurrentDirectory + @"\HinhAnh"))
                {
                    System.IO.Directory.CreateDirectory(Environment.CurrentDirectory + @"\HinhAnh");
                }

                if (Destination == Source)
                {
                    pbHinh.Image = Image.FromFile(ofd.FileName);
                }
                else
                {
                    System.IO.File.Copy(Source, Destination, true);
                    pbHinh.Image = Image.FromFile(ofd.FileName);
                }
            }
        }
       
        private void btnTao_Click(object sender, EventArgs e)
        {
            try
            {   //xử lý ngày
                string[] arrngay = (dtiNgayNhap.Text).Split('/');
                if (arrngay.Length == 1)
                {
                    MessageBox.Show("Vui lòng nhập ngày");
                }
                else
                {
                    BenhNhan_DTO bn = new BenhNhan_DTO();
                    bn.TenBN = txtTen.Text;
                    if (rdNam.Checked == true)
                        bn.GioiTinh = 1;
                    else
                        bn.GioiTinh = 0;
                    bn.CMND = txtCMND.Text;
                    bn.DiaChi = txtDiaChi.Text;
                    bn.SoDienThoai = txtSoDienThoai.Text;
                    bn.NgheNghiep = txtNgheNghiep.Text;
                    //xử lý ngày
                    string ngayformat = arrngay[1] + "/" + arrngay[0] + "/" + arrngay[2];
                    bn.NgaySinh = ngayformat;

                    if (rdCo.Checked == true)
                        bn.TheBH = 1;
                    else
                        bn.TheBH = 0;
                    bn.Hinh = fileName;
                    bn.Mota = "";
                    bn.Hinh = fileName;
                    if (BenhNhan_BUS.ThemBenhNhan(bn) == true)
                    {
                        MessageBox.Show("Thêm bệnh nhân thành công");
                        if (KhiThemMoi != null)
                            KhiThemMoi();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}